package com.xbongbong.saas.factory.publicrule;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xbongbong.saas.constant.BasicConstant;
import com.xbongbong.saas.domain.entity.TransferBackRuleSeniorEntity;
import com.xbongbong.saas.enums.CustomerSeniorRuleTypeEnum;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Objects;

import static com.xbongbong.saas.factory.publicrule.OperateCheckRuleValue.checkClueLabelIds;
import static com.xbongbong.saas.factory.publicrule.OperateCheckRuleValue.checkSpecialUser;

/**
 * 公海高级模式 - 其他设置中 最大客户（线索）数 、前负责人、捞取频率，日志获取
 *
 * @author : 杨磊
 * @date 创建时间：2022-05-31 下午4:24:22
 */
@Component
public class OperateSeniorOtherSetRuleValue {

    /**
     * 获取特殊员工
     * @param ruleObject
     * @return
     */
    public List<String> getAllSpecialUser(JSONObject ruleObject) {
        return checkSpecialUser(ruleObject);
    }

    /**
     * 获取规则配置中的标签Id
     * @param ruleObject
     * @return
     */

    public List<Long> getCustomerLabelIds(Object ruleObject) {
        return checkClueLabelIds(ruleObject);
    }

    /**
     * 获取规则状态
     * @param entity
     * @return
     */
    public Integer getStatus(TransferBackRuleSeniorEntity entity) {
        if (entity != null) {
            return entity.getStatus();
        }
        return null;
    }

    /**
     * 有无合同
     * @param ruleValue
     * @return
     */
    public Integer getContract(JSONObject ruleValue) {
        if (ruleValue == null) {
            return null;
        }
        return ruleValue.getInteger("contract");
    }

    /**
     * 有无机会
     * @param ruleValue
     * @return
     */
    public Integer getOpportunity(JSONObject ruleValue) {
        if (ruleValue == null) {
            return null;
        }
        return ruleValue.getInteger("opportunity");
    }

    /**
     * 客户、线索状态
     * @param ruleValue
     * @return
     */
    public JSONArray getCustomerStatus(JSONObject ruleValue,Integer businessType) {
        if (ruleValue == null) {
            return null;
        }
        String statusKey = "customerStatus";
        if (Objects.equals(businessType, XbbRefTypeEnum.CLUE.getCode())){
            statusKey = "clueStatus";
        }
        return ruleValue.getJSONArray(statusKey);
    }

    /**
     * 客户、线索类型
     * @param ruleValue
     * @return
     */
    public Integer getType(JSONObject ruleValue) {
        if (ruleValue == null) {
            return null;
        }
        return ruleValue.getInteger("type");
    }

    /**
     * 退回规则
     * @param entity
     * @return
     */
    public JSONArray getRestrict(TransferBackRuleSeniorEntity entity) {
        if (entity == null) {
            return null;
        }
        Integer type = entity.getType();
        JSONArray ruleValue = entity.getRuleValue();
        if (Objects.equals(type, CustomerSeniorRuleTypeEnum.MAX_CUSTOMER_NUM_RULE.getCode())){
            return ruleValue.getJSONObject(BasicConstant.ZERO).getJSONArray("restrict");
        }else {
            return ruleValue;
        }
    }

    /**
     *
     * @param ruleValue
     * @return
     */
    public JSONObject getBackRule(JSONObject ruleValue) {
        if (ruleValue == null) {
            return null;
        }
        return ruleValue.getJSONObject("backRule");
    }

}
